<?php
 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../../document/class_corpus.php";
 require "../../document/class_document.php";
 require "../../document/class_document_xml.php";

 require "../../files/tool_files_dirs.php";

 require "../../misc/class_tag.php";
 require "../../misc/tool_color.php";

 require "../../tree/class_tree.php";
 require "../../tree/class_tree_xhtml.php";
 require "../../tree/class_structure.php";
 require "../../tree/tool_tree.php";

 require "../../array/tool_array.php";

 require "../../pear_graph/tool_pear_graph.php";

 require "../../maths/tool_maths.php";

 require "../../string/tool_string.php";
 require "../../string/class_segment.php";

 require "../../pdo/class_sql_document.php";

function fill_graph(&$graph,$data_x,$data_y){
  fill_graph_aux($graph,$data_x,$data_y);
  return $graph->plot4();
}

function fill_graph_aux(&$graph,$data_x,$data_y){
  $graph->__add_array_x($data_x);
  $graph->__add_array_y($data_y);
}

//$exp=new sql_experience("align_word00");
//$exp=new sql_experience("align_word_exp01");
$exp=new sql_experience("align_ecr_word");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$data_lg=$array_obj_sql['sql_document']->reader_all_lg();

//$data_lg=array(
//  array('lg'=>"fr"), array('lg'=>"es"),
//  array('lg'=>"fi"), array('lg'=>"hu")
//);

foreach($data_lg as $ligne){
  $array_id_document=$array_obj_sql['sql_document']->reader_lg2array_id_document_id_bloc($ligne['lg']);
  $correspondance_doc_bloc=array();
  $correspondance_bloc_doc=array();
  foreach($array_id_document as $ligne_doc){
    $correspondance_doc_bloc[$ligne_doc['id_document']][]=$ligne_doc['id_bloc'];
    $correspondance_bloc_doc[$ligne_doc['id_bloc']]=$ligne_doc['id_document'];
  }
  $d=$array_obj_sql['sql_rstr']->reader_all2count_bloc($ligne['lg']);
  $repartition_bloc=$repartition_global_len=array();
  $repartition_global_freq=$repartition_doc_freq=array();
  foreach($d as $l){
    $id_doc=$correspondance_bloc_doc[$l['id_bloc']];
    $repartition_bloc[$l['graphie']][$l['id_bloc']]=$l['COUNT(r.id_bloc)'];
    $repartition_global_len[$l['len']]+=$l['COUNT(r.id_bloc)'];
    $repartition_doc_len[$id_doc][$l['len']]+=$l['COUNT(r.id_bloc)'];
    $repartition_global_freq[$l['graphie']]+=$l['COUNT(r.id_bloc)'];
    $repartition_doc_freq[$id_doc][$l['graphie']]+=$l['COUNT(r.id_bloc)'];
  }

  $zipf=tool_maths::zipf($repartition_global_freq);

  $array_data[$ligne['lg']]=array(
    $correspondance_bloc_doc,
    $correspondance_doc_bloc,
    $repartition_bloc,
    $repartition_global_len,
    $repartition_doc_len,
    $repartition_global_freq,
    $repartition_doc_freq
  );
}

$motif_lg="fr";

$data_motif=$array_data[$motif_lg];
//print_r($data_motif[6]);
//die();

function rep2log_rep(&$rep){
  $rep_log=array();
  $cpt=0;
  foreach($rep as $val){
    ++$cpt;
    $lcpt="".log($cpt);
    if($val==0) $lval="-0.2";
    else $lval="".log($val); 
    $rep_log[$lcpt]=$lval;
  }
  return $rep_log;
}

$tool_graph=new Tool_pear_graph();
$tool_graph->__set_width(500);
$tool_graph->__set_height(510);
$tool_graph->__add_title("Loi de Zipf");

foreach($array_data as $motif_lg=>$data_motif){
  $corres_doc_bloc=$data_motif[1];
  $rep_doc_freq=$data_motif[6];
  $rep_global_freq=$data_motif[5];
  arsort($rep_global_freq);
  $zipf=tool_maths::zipf($rep_global_freq);
  $zipf_log=tool_maths::zipf_log($zipf);
  $x = array_keys($zipf_log);
  $y = array_values($zipf_log);
  $regression=tool_maths::regression_lineaire_simple($x,$y);

  $first_x=reset($x);
  $first_y=$regression["a"]*$first_x+$regression["b"];
  $last_x=end($x);
//  $last_y=$regression["a"]*$last_x+$regression["b"];
  $last_y=0;
  $last_x = -$regression["b"] / $regression["a"];
  $new_x = array($first_x,$last_x);
  $new_y = array($first_y,$last_y);

  fill_graph_aux($tool_graph,$new_x,$new_y);
  $tool_graph->__add_plot_title($motif_lg);
//  $tool_graph->__add_plot_color("#FF0000");

//  $graph=$tool_graph->plot_log_log01();
  $graph=$tool_graph->line01();
  ob_start();
    $graph->done();
    $graph_svg=ob_get_contents();
  ob_clean();
}
Tool_files::file_write("./all_regression.svg",$graph_svg);
?>
